home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / enscript.4 / enscript / enscript-1.4.0 / src / xalloc.c < prev   
Encoding:
C/C++ Source or Header  |  1996-02-25  |  1.7 KB  |  95 lines

  1. /* 
  2.  * Non-failing memory allocation routines.
  3.  * Copyright (c) 1995 Markku Rossi.
  4.  *
  5.  * Author: Markku Rossi <mtr@iki.fi>
  6.  */
  7.  
  8. /*
  9.  * This file is part of GNU enscript.
  10.  * 
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2, or (at your option)
  14.  * any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; see the file COPYING.  If not, write to
  23.  * the Free Software Foundation, 59 Temple Place - Suite 330,
  24.  * Boston, MA 02111-1307, USA.
  25.  */
  26.  
  27. #include "gsint.h"
  28.  
  29. /*
  30.  * Global functions.
  31.  */
  32.  
  33. void *
  34. xmalloc (size_t size)
  35. {
  36.   void *ptr;
  37.  
  38.   ptr = malloc (size);
  39.   if (ptr == NULL)
  40.     fatal (_("xmalloc(): couldn't allocate %d bytes\n"), size);
  41.  
  42.   return ptr;
  43. }
  44.  
  45.  
  46. void *
  47. xcalloc (size_t num, size_t size)
  48. {
  49.   void *ptr;
  50.  
  51.   ptr = calloc (num, size);
  52.   if (ptr == NULL)
  53.     fatal (_("xcalloc(): couldn't allocate %d bytes\n"), size);
  54.  
  55.   return ptr;
  56. }
  57.  
  58.  
  59. void *
  60. xrealloc (void *ptr, size_t size)
  61. {
  62.   void *nptr;
  63.  
  64.   if (ptr == NULL)
  65.     return xmalloc (size);
  66.  
  67.   nptr = realloc (ptr, size);
  68.   if (nptr == NULL)
  69.     fatal (_("xrealloc(): couldn't reallocate %d bytes\n"), size);
  70.  
  71.   return nptr;
  72. }
  73.  
  74.  
  75. void 
  76. xfree (void *ptr)
  77. {
  78.   if (ptr == NULL)
  79.     return;
  80.  
  81.   free (ptr);
  82. }
  83.  
  84.  
  85. char *
  86. xstrdup (char *str)
  87. {
  88.   char *tmp;
  89.  
  90.   tmp = xmalloc (strlen (str) + 1);
  91.   strcpy (tmp, str);
  92.  
  93.   return tmp;
  94. }
  95.